package com.seeseafish.mongo.dao.mongo;

import com.seeseafish.mongo.domain.ApiLogDomain;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by chencheng on 2016/8/16.
 */
@Repository
public class HydInterfaceLogDAO extends MongodbDAO<ApiLogDomain>{

    /**
     * 根据查询query 查找list
     *
     * @param query
     * @return
     */
    public List<ApiLogDomain> find(Query query) {
        Sort sort = new Sort(Sort.Direction.DESC, "requestTime");
        query.with(sort);
        return super.find(query);
    }

    /**
     * 修改日志
     * @param update
     */
    public void updateByLogId(Update update, String logId) {
        Criteria criteria = Criteria.where("logId").is(logId);
        Query query = new Query(criteria);
        super.update(query, update);
    }

    /**
     * 根据logId 查找一个对象
     *
     * @param logId
     * @return
     */
    public ApiLogDomain findOneByLogId(String logId){
        Criteria criteria = Criteria.where("logId").is(logId);
        Query query = new Query(criteria);
        return super.findOne(query);
    }

    /**
     * 根据logId 删除对象
     *
     * @param logId
     */
    public void deleteByLogId(String logId) {
        Criteria criteria = Criteria.where("logId").is(logId);
        Query query = new Query(criteria);
        super.delete(query);
    }

}
